function countLen(num, count) {
	let len = 0,
		numArr = num.toString().split('.');
	len = numArr[1].length;
	if (Number(numArr[0]) == 0) {
		numArr[0] = 1;
		count++;
	}
	num = numArr.join('.');
	return {
		num: num,
		len: len,
		count: count
	}
}

function addZero(len) {
	let str = '';
	for (let i = 0; i < len; i++) {
		str += '0';
	}
	return str;
}

function basics(num1, num2, operator) {
	let t1, t2, r1, r2, count = 0;
	try {
		let v = countLen(num1, count);
		t1 = v.len;
		num1 = v.num;
		count = v.count;
	} catch (e) {
		t1 = 0;
	}
	try {
		let v = countLen(num2, count);
		t2 = v.len;
		num2 = v.num;
		count = v.count;
	} catch (e) {
		t2 = 0;
	}
	let t = Math.max(t1, t2);
	let str1 = num1.toString(),
		str2 = num2.toString();
	str1 = str1 + addZero(t - t1);
	str2 = str2 + addZero(t - t2);
	r1 = Number(str1.replace(".", ""));
	r2 = Number(str2.replace(".", ""));
	let handleResult = 0;
  let result;
	if (operator == '-') {
		handleResult = r1 - r2;
    result = (handleResult + count * Math.pow(10, t)) / Math.pow(10, t);
	} else {
		handleResult = r1 + r2;
    result = (handleResult - count * Math.pow(10, t)) / Math.pow(10, t);
	}
  //	let result = (handleResult - count * Math.pow(10, t)) / Math.pow(10, t);
	return result;
}

// 两数相减
function subtract(num1, num2) {
	//return basics(num1, num2, '-');
  return num1 -  num2;
}

// 两数相加
function added(num1, num2) {
	let result = basics(num1, num2);
	return result;
}

//两数相乘
function accMul(num1, num2) {
	let m = 0, s1 = num1.toString(), s2 = num2.toString();
	try {
		m += s1.split(".")[1].length
	} catch (e) {};
	try {
		m += s2.split(".")[1].length
	} catch (e) {};
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

// 两数相除
function accDiv(num1, num2) {
	let t1, t2, r1, r2;
	try {
		t1 = num1.toString().split('.')[1].length;
	} catch (e) {
		t1 = 0;
	}
	try {
		t2 = num2.toString().split(".")[1].length;
	} catch (e) {
		t2 = 0;
	}
	r1 = Number(num1.toString().replace(".", ""));
	r2 = Number(num2.toString().replace(".", ""));
	return (r1 / r2) * Math.pow(10, t2 - t1);
}

export default {
	subtract,
	added,
	accMul,
	accDiv
}
